package mysql

import "gorm.io/gorm"

type Takeout struct {
	gorm.Model
	Name  string  `gorm:"type:varchar(30);comment:餐品名称"`
	Price float64 `gorm:"type:decimal(10,2);comment:餐品价格"`
	Num   int     `gorm:"type:int;comment:餐品库存"`
	Types int     `gorm:"type:int;comment:餐品分类:1-精品推荐,2-人气热卖,3-精选套餐,4-所有菜单，5-主食"`
}

func (Takeout) TableName() string {
	return "takeout"
}

func (t *Takeout) Find(types int) (result []Takeout, err error) {
	err = DB.Where("types=?", types).Find(&result).Error
	if err != nil {
		return nil, err
	}
	return result, nil
}

func (t *Takeout) FindId(takeoutId int) (result Takeout, err error) {
	err = DB.Where("id=?", takeoutId).Find(&result).Error
	if err != nil {
		return Takeout{}, err
	}
	return result, nil
}

func (t *Takeout) UpdatedNum(buyNum int, TakeoutId int) error {
	return DB.Where("id=?", TakeoutId).Find(&t).Update("num", t.Num-buyNum).Error
}
